﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace FremtidensPlejehjem.Messaging
{
    public class Producer
    {
        MessageQueue _channel;

        public Producer(MessageQueue channel)
        {
            _channel = channel;
        }

        public void SendMessage(object message)
        {
            string xmlMessage = SerializeMessage(message);

            _channel.Send(xmlMessage);
        }

        private string SerializeMessage(object message)
        {
            string result = "";

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(message.GetType());
            StringWriter sw = new StringWriter();
            XmlWriter xw = XmlWriter.Create(sw);

            x.Serialize(xw, message);

            result = sw.ToString();

            return result;
        }
    }
}
